﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PromotionApp.Utility;
using PromotionApp.Model;
using System.Data.SqlClient;
using System.Data;

namespace PromotionApp.DAL
{
    public class TopicDAL : BaseDAL
    {
        #region Private Variable
        
        private static TopicDAL _TopicDAL = null;

        #endregion

        #region Propery
        #endregion

        #region Constructor

        private TopicDAL() : base()
        {
            
        }

        #endregion

        #region Public Method

        public void UpdateTopicStatus(int topicId, string strStatusCode, SqlConnection conn)
        {
            string strSql = "TxTopicUpdateStatus";
            List<SqlParameter> list = new List<SqlParameter>();
            list.Add(new SqlParameter("TopicId", topicId));
            list.Add(new SqlParameter("StatusCode", strStatusCode));
            this._SQLHelper.ExecuteNonQueryTransaction(strSql, list, CommandType.StoredProcedure, conn);            
        }

        public DataSet GetTopicById(int topicId)
        {
            string strSql = "TxTopicSelectById";

            List<SqlParameter> list = new List<SqlParameter>();
            list.Add(new SqlParameter("TopicId", topicId));
            DataSet ds = this._SQLHelper.GetDataSet(strSql, list, CommandType.StoredProcedure);

            return ds;
        }

        public void AddTopic(Topic topic, SqlConnection conn)
        {
            string strSql = "TxTopicInsert";
            List<SqlParameter> list = new List<SqlParameter>();

            list.Add(new SqlParameter("CategoryId", topic.CategoryId));
            list.Add(new SqlParameter("TopicDescription", topic.TopicDescription));
            list.Add(new SqlParameter("Message", topic.Message));
            list.Add(new SqlParameter("StatusCode", topic.StatusCode));

            if (topic.ImagePath.Trim().Length > 0)
                list.Add(new SqlParameter("ImagePath", topic.ImagePath));                    
            else
                list.Add(new SqlParameter("ImagePath", System.DBNull.Value));
                        
            if (topic.UserName.Trim().Length > 0)
                list.Add(new SqlParameter("UserName", topic.UserName));
            else
                list.Add(new SqlParameter("UserName", System.DBNull.Value));

            if (topic.Email.Trim().Length > 0)
                list.Add(new SqlParameter("Email", topic.Email));                    
            else
                list.Add(new SqlParameter("Email", System.DBNull.Value));                    
            
            this._SQLHelper.ExecuteNonQueryTransaction(strSql, list, CommandType.StoredProcedure, conn);
        }        

        public static TopicDAL GetInstance()
        {
            if (_TopicDAL == null)
                _TopicDAL = new TopicDAL();

            return _TopicDAL;
        }

        #endregion
    }
}